package me.alex.koulingbao.ui.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.kongzue.baseframework.BaseActivity;
import com.kongzue.baseframework.interfaces.DarkStatusBarTheme;
import com.kongzue.baseframework.interfaces.Layout;
import com.kongzue.baseframework.interfaces.NavigationBarBackgroundColorHex;
import com.kongzue.baseframework.util.JumpParameter;
import com.kongzue.dbv3.DB;
import com.kongzue.dbv3.data.DBData;
import com.kongzue.dialog.interfaces.OnDialogButtonClickListener;
import com.kongzue.dialog.util.BaseDialog;
import com.kongzue.dialog.v3.CustomDialog;
import com.kongzue.dialog.v3.MessageDialog;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import me.alex.koulingbao.base.DefaultAdapter;
import me.alex.koulingbao.R;
import me.alex.koulingbao.ui.adapter.KouLingAdapter;

@NavigationBarBackgroundColorHex("#FEDCE3")//导航栏颜色
@DarkStatusBarTheme(true)
@Layout(R.layout.activity_main)
public class MainActivity extends BaseActivity {

    KouLingAdapter adapter;
    TextView add;
    RecyclerView recyclerView;
    List<DBData> dataList = new ArrayList<>();
    ClipboardManager cm;

    @Override
    public void initViews() {
        cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        add = findViewById(R.id.main_add);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAddDialog();
            }
        });
        recyclerView = findViewById(R.id.main_rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(me));
        recyclerView.setHasFixedSize(true);
        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(Objects.requireNonNull(ContextCompat.getDrawable(this, R.drawable.custom_divider)));
//        recyclerView.addItemDecoration(divider);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        adapter = new KouLingAdapter(new ArrayList<>());
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new DefaultAdapter.OnRecyclerViewItemClickListener<DBData>() {
            @Override
            public void onItemClick(@NonNull View view, int viewType, @NonNull DBData data, int position) {
                switch (view.getId()) {
                    case R.id.into_go:
                        //跳转
                        ClipData mClipData = ClipData.newPlainText("Label", data.getString("kouling"));
                        cm.setPrimaryClip(mClipData);
                        runDelayed(new Runnable() {
                            @Override
                            public void run() {
                                data.set("date", System.currentTimeMillis());
                                DB.getTable("kouling").update(data);
                                openApp(data.getString("type"));
                                adapter.notifyItemChanged(position);
                            }
                        }, 200);
                        break;
                    case R.id.into_content:
                        //获取剪贴板管理器：
                        ClipData cv = ClipData.newPlainText("Label", data.getString("kouling"));
                        cm.setPrimaryClip(cv);
                        toast("复制口令成功");
                        break;
                }
            }
        });
        adapter.setOnItemLongClickListener(new DefaultAdapter.OnRecyclerViewItemLongClickListener<DBData>() {
            @Override
            public void onItemLongClick(@NonNull View view, int viewType, @NonNull DBData data, int position) {
                MessageDialog.show(me, "提示", "是否删除当前口令")
                        .setOnOkButtonClickListener(new OnDialogButtonClickListener() {
                            @Override
                            public boolean onClick(BaseDialog baseDialog, View v) {
                                deleteData(data, position);
                                return false;
                            }
                        });
            }
        });
    }

    private void deleteData(DBData data, int position) {
        DB.getTable("kouling").delete(data);
        adapter.getInfos().remove(position);
        adapter.notifyDataSetChanged();
        toast("删除成功");
    }

    private void showAddDialog() {
        CustomDialog.show(me, R.layout.dialog_add_kouling, new CustomDialog.OnBindView() {
            @Override
            public void onBind(final CustomDialog dialog, View v) {
                final String[] type = {"com.taobao.taobao"};
                EditText addTitle = v.findViewById(R.id.add_title);
                EditText addContent = v.findViewById(R.id.add_content);
                Spinner addType = v.findViewById(R.id.add_type);
                TextView addSubmit = v.findViewById(R.id.add_submit);
                ArrayAdapter<String> adapter = new ArrayAdapter<>(me, R.layout.item_type, (getResources().getStringArray(R.array.nameArray)));
                addType.setAdapter(adapter);
                addType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        type[0] = getResources().getStringArray(R.array.packageArray)[position];
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
                addSubmit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (addContent.getText().toString().isEmpty()) {
                            toast("口令不能为空");
                            return;
                        }
                        DBData data = new DBData();
                        data.set("title", addTitle.getText().toString());
                        data.set("kouling", addContent.getText().toString());
                        data.set("type", type[0]);
                        data.set("date", 0);
                        addData(data);
                        dialog.doDismiss();
                        toast("添加成功");
                    }
                });
            }
        });
    }


    @Override
    public void initDatas(JumpParameter parameter) {
        dataList = DB.getTable("kouling").find();
        adapter.setData(dataList);
    }

    @Override
    public void setEvents() {

    }

    private void addData(DBData data) {
        DB.getTable("kouling")
                .setAllowDuplicate(true)       //允许添加重复数据
                .add(data);
        adapter.getInfos().add(0, data);
        adapter.notifyDataSetChanged();
    }
}